package org.jks.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.exception.ConstraintViolationException;
import org.jks.config.Config;
import org.jks.excel.SparePartsUtil;
import org.jks.model.DeviceType;
import org.jks.model.Flight;
import org.jks.model.SpareParts;
import org.jks.report.model.SparePartsModel;
import org.jks.request.model.Ids;
import org.jks.request.model.UISpareparts;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.DeviceInOutService;
import org.jks.service.DeviceService;
import org.jks.service.DeviceTypeService;
import org.jks.service.ForApplyService;
import org.jks.service.SparePartsService;
import org.jks.util.JsonUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/spareparts")
public class SparePartsController {
	@Autowired
	SparePartsService sparePartsService;
	@Autowired 
	DeviceService deviceService;
	@Autowired
	DeviceTypeService deviceTypeService;
	@Autowired
	DeviceInOutService deviceInOutService;
	@Autowired
	ForApplyService forApplyService;

	@RequestMapping("/add")
	public void add(UISpareparts uiSpareparts, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		ActionResult ar = new ActionResult();
		try {
			SpareParts spareparts = new SpareParts();
			DeviceType deviceType = deviceTypeService.findById(uiSpareparts.getDeviceTypeId());
			spareparts.setDeviceType(deviceType);
			spareparts.setCount(deviceService.countByTypeForSparePart(deviceType));
			Flight flight = new Flight();
			flight.setId(uiSpareparts.getGroupId());
			spareparts.setGroup(flight);
			spareparts.setMinInventory(uiSpareparts.getMinInventory());
			sparePartsService.save(spareparts);
			ar.setSuccess(true);
			ar.setData(spareparts);
		} 
		catch( ConstraintViolationException e){
			ar.setMsg("备品名称冲突");
		}
		catch (Exception e) {
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	public void add(SpareParts spareparts, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		try {
			sparePartsService.save(spareparts);
			ar.setSuccess(true);
			ar.setData(spareparts);
		} catch (Exception e) {
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, HttpServletResponse response, HttpSession session){
		ActionResult ar = new ActionResult();
		try{
			sparePartsService.delete(ids.getIds());
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			sparePartsService.delete(id);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable long id, HttpServletRequest request, HttpSession session, HttpServletResponse response, Map<String,Object> model){
		try {
			SpareParts spareparts = sparePartsService.findById(id);
			model.put("spareparts", JsonUtil.Object2Json(spareparts));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("wuziMgr/bybp_detail", model);
	}
	
	@RequestMapping("/update")
	public void update(UISpareparts uiSpareparts, HttpServletRequest request, HttpServletResponse response, HttpSession session){
		ActionResult ar = new ActionResult();
		try{
			SpareParts spareparts = sparePartsService.findById(uiSpareparts.getId());
			DeviceType deviceType = deviceTypeService.findById(uiSpareparts.getDeviceTypeId());
			spareparts.setDeviceType(deviceType);
			spareparts.setCount(deviceService.countByTypeForSparePart(deviceType));
			Flight flight = new Flight();
			flight.setId(uiSpareparts.getGroupId());
			spareparts.setGroup(flight);
			spareparts.setMinInventory(uiSpareparts.getMinInventory());
			sparePartsService.update(spareparts);
			ar.setSuccess(true);
		}
		catch( ConstraintViolationException e){
			ar.setMsg("备品名称冲突");
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = sparePartsService.count();
			List<SpareParts> spareParts = sparePartsService.list(page, pagesize);
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(spareParts);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/exportAll")
	public void exportAll(HttpServletRequest request, HttpServletResponse response){
		List<SpareParts> tempList = sparePartsService.all();
		List<SparePartsModel> models = new ArrayList<SparePartsModel>();
		for(SpareParts s : tempList)
		{
			models.add(s.toModel());
		}
		SparePartsUtil.export(response, models);
	}
	
	
}
